package com.storlead.shiro.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.thymeleaf.expression.Maps;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Author: Timo
 * @Date: 2019/2/13 15:06
 * @Description: Shiro 配置文件
 */
@Slf4j
@Configuration
public class ShiroConfig {


    public Map<String,String> getFilterChainDefinitionMap(){
        Map<String,String> filterMap = new LinkedHashMap<String,String>();
        filterMap.put("/static/**", "anon");
        //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了
        filterMap.put("/user/logout", "logout");
        // 授权过滤器
        filterMap.putAll(getUserRoleMap());
        /*
        filterMap.put("/user/update","perms[user:update]");
        filterMap.put("/user/add","perms[user:add]");
        filterMap.put("/user/del","perms[user:del]");
        filterMap.put("/user/test","perms[user:test]");*/
        //<!-- 过滤链定义，从上向下顺序执行，一般将/**放在最为下边 -->:这是一个坑呢，一不小心代码就不好使了;
        //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
        filterMap.put("/**", "authc");



        return filterMap;
    }

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(){
        log.info("拦截器-》ShiroConfig.shiroFilter");
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager());
        shiroFilterFactoryBean.setLoginUrl("/user/login");
        shiroFilterFactoryBean.setSuccessUrl("/index");
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(getFilterChainDefinitionMap());


        return shiroFilterFactoryBean;
    }

   /* @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition(){
        log.info("拦截器-》ShiroConfig.shiroFilterChainDefinition");
        DefaultShiroFilterChainDefinition chain = new DefaultShiroFilterChainDefinition();
        chain.addPathDefinition("/user/login","anon");
        chain.addPathDefinition("/static/*","anon");
        chain.addPathDefinition("/**","authc");
        return chain;
    }*/

    @Bean
    public MyRealm myRealm(){
        log.info("拦截器-》ShiroConfig.myRealm");
        MyRealm myRealm = new MyRealm();
        return myRealm;
    }

    @Bean
    public DefaultSecurityManager securityManager(){
        log.info("拦截器-》ShiroConfig.securityManager");
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myRealm());
        return securityManager;
    }

    @Bean
    public KickoutSessionFilter kickoutSessionFilter(){
        return new KickoutSessionFilter();
    }



    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
        log.info("ShiroConfig.authorizationAttributeSourceAdvisor");
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    public SimpleMappingExceptionResolver resolver(){
        SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
        Properties properties = new Properties();
        properties.setProperty("org.apache.shiro.authz.UnauthorizedException", "/403");
        resolver.setExceptionMappings(properties);
        return resolver;
    }

    @Bean
    public UrlPermissionsFilter urlPermissionsFilter(){
        return new UrlPermissionsFilter();
    }

    private Map<String,String> getUserRoleMap(){
        Map<String,String> resultMap = new HashMap<>();
        resultMap.put("/user/get","perms[user:get]");
        resultMap.put("/user/test","perms[user:test]");
        resultMap.put("/user/add","perms[user:add]");
        resultMap.put("/user/update","perms[user:update]");
        resultMap.put("/user/del","perms[user:del]");
        resultMap.put("/user/del","perms[*:*]");
        return resultMap;
    }


}
